package com.tanhua.dubbo.api;

import com.tanhua.dubbo.utils.IdWorker;
import com.tanhua.model.mongo.FocusUser;
import com.tanhua.model.mongo.Video;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class VideoApiImpl implements VideoApi{

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IdWorker idWorker;

    @Override
    public String save(Video video) {
        //1、设置属性
        video.setVid(idWorker.getNextId("video"));
        video.setCreated(System.currentTimeMillis());
        //2、调用方法保存对象
        mongoTemplate.save(video);
        //3、返回对象id
        return video.getId().toHexString();
    }

    @Override
    public List<Video> findMovementsByVids(List<Long> vids) {
        Query query = Query.query(Criteria.where("vid").in(vids));
        return mongoTemplate.find(query,Video.class);
    }

    @Override
    public List<Video> queryVideoList(int page, Integer pagesize) {
        Query query = new Query().limit(pagesize).skip((page -1) * pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        return mongoTemplate.find(query,Video.class);
    }

    @Override
    public PageResult findByUserId(Integer page, Integer pagesize, Long userId) {
        Query query = Query.query(Criteria.where("userId").in(userId));
        long count = mongoTemplate.count(query, Video.class);
        query.limit(pagesize).skip((page -1) * pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        List<Video> list = mongoTemplate.find(query, Video.class);
        return new PageResult(page,pagesize,count,list);
    }



    // TODO  VIDEO
    @Override
    public Video findById(String id) {
        Query query = Query.query(Criteria.where("id").is(id));
        return mongoTemplate.findOne(query, Video.class); //判断数据是否存在
    }
    @Override
    public Integer updateliked(Video video) {
        Query query=new Query(Criteria.where("id").is(video.getId()));
        Update update = new Update().inc("likeCount", 1);
        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);//获取更新后的最新数据
        Video  modify = mongoTemplate.findAndModify(query, update, options, Video.class);
        //统计当前评论的点赞总数
        return modify.getLikeCount();
    }

    @Override
    public Integer updatedislike(Video video) {
        Query query=new Query(Criteria.where("id").is(video.getId()));
        Update update = new Update().inc("likeCount", -1);
        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);//获取更新后的最新数据
        Video  modify = mongoTemplate.findAndModify(query, update, options, Video.class);
        //统计当前评论的点赞总数
        return modify.getLikeCount();
    }

    //关注用户
    public void saveFollowUser(FocusUser focusUser) {

        //判断数据库中是否有记录,如果没有就保存
        focusUser.setId(ObjectId.get());
        focusUser.setCreated(System.currentTimeMillis());
        mongoTemplate.save(focusUser);
    }

   //取消关注用户
    public void deleteFollowUser(Long userId, Long followUserId) {
        Criteria criteria = Criteria.where("userId").is(userId)
                .and("followUserId").is(followUserId);
        mongoTemplate.remove(new Query(criteria),FocusUser.class);
    }
}
